﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SachOnlineLibrary.BLL;
using SachOnlineLibrary.Public;
public partial class WebUserControlThemGioHang : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        PhieuDatHangBLL lib = new PhieuDatHangBLL();      
        if (!IsPostBack)
        {
            if (Session["DangNhap"] == null)
            {
                Response.Redirect("WebFormGioHang.aspx");
            }
            else
            {
                Sach item = new Sach();
                item = lib.LaySach(int.Parse(Request.QueryString.Get("MaSach").ToString()));
                List<Sach> listsach = new List<Sach>();
                if (Session["Cart"] == null)
                {
                    item.SoLuong = 1;
                    listsach.Add(item);
                    Session["Cart"] = listsach;
                }
                else
                {
                    bool flag = false;
                    listsach = (List<Sach>)Session["Cart"];
                    foreach (Sach s in listsach)
                    {
                        if (s.MaSach==item.MaSach)
                        {
                            s.SoLuong++;
                            flag=true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        item.SoLuong = 1;
                        listsach.Add(item);
                    }
                    Session["Cart"] = listsach;
                }
                Response.Redirect("SACHGIALAP.aspx");
            }
        }
    }
}
